package com.xsh.gls.controller;

import com.xsh.gls.interceptor.MyHandlerInterceptor;
import com.xsh.gls.pojo.Customer;
import com.xsh.gls.pojo.Transfer;
import com.xsh.gls.pojo.Type_Functional;
import com.xsh.gls.pojo.User;
import com.xsh.gls.service.CustomerService;
import com.xsh.gls.service.Type_FunctionalService;
import com.xsh.gls.service.UserService;
import com.xsh.gls.utils.Constant;
import com.xsh.gls.utils.RedisUtils;
import com.xsh.gls.utils.SecurityUtils;
import com.xsh.gls.utils.restDemo.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/first")
public class LoginController extends MyHandlerInterceptor {
        @Autowired
        private UserService userService;
        @Autowired
        private RedisUtils redisUtils;
        @Autowired
         private CustomerService customerService;
        @Autowired
        private Type_FunctionalService type_functionalService;
    @RequestMapping("/login")
    public Transfer login(String userName , String pwd, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        User user=null;
        List<Type_Functional> funList=null;
        String token=UUID.randomUUID().toString().replace("-","");
        System.out.println("======token:" + token);
        Transfer transfer=new Transfer();
        try {
          user=userService.queryUserByUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(user==null){
            transfer.setMessage("no");

        }else {
            if(user.getPassword().equals(SecurityUtils.md5Hex(pwd))){
                try {
                    funList= type_functionalService.queryFunctionByTypeId(user.getTypeid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                user.setFunList(funList);
                redisUtils.set(token,user);
                transfer.setMessage("yes");
                transfer.getMap().put("token",token);
            }else{
                transfer.setMessage("nopwd");
            }
        }
        return transfer;
    }
    @RequestMapping("/miss")
    public Transfer missPwd(String userName , String phone , String pwd,String code,HttpServletResponse response, HttpServletRequest request) {
        User user = null;
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Transfer transfer = new Transfer();
        try {
            user = userService.queryUserByUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
            transfer.setMessage("no");
        }
        if (user == null) {
            transfer.setMessage("no");
        } else {
                System.out.println(redisUtils.getKey(phone)+"\t"+user.getPhone()+"\t"+phone+"\t"+code);
                if (user.getPhone().equals(phone) && code.equals(redisUtils.getKey(phone))) {
                    user.setPassword(pwd);
                    System.out.println("用户的ID:\t\t\t"+user.getId());
                    System.out.println(userService.updateByPrimaryKeySelective(user));
                    if (userService.updateByPrimaryKeySelective(user) > 0) {
                        transfer.setMessage("yes");
                    }else {
                        transfer.setMessage("no");
                    }

                } else {
                    transfer.setMessage("no");
                }
            }

        return transfer;

    }

    @RequestMapping("/getCode")
    public Transfer getCode(String userName , String phone, HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        User user = null;
        Transfer transfer = new Transfer();
        try {
            user = userService.queryUserByUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
            transfer.setMessage("no");
        }
        if (user == null) {
            transfer.setMessage("no");
        } else {
            if (user.getPhone().equals(phone)){
                String yanz = Rest.SimpleParamSend(phone,user.getId().toString());
                System.out.println(yanz);

                if (yanz != null){
                    redisUtils.setStr(phone,yanz,60L);
                    transfer.setMessage("yes");

                }else {
                    transfer.setMessage("no");
                }
            }else {
                transfer.setMessage("noPhone");
            }
        }
        return transfer;

    }
    @RequestMapping("/getCode2")
    public Transfer getCode2(String phone, HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
            Transfer transfer = new Transfer();
            String yanz = Rest.SimpleParamSend(phone,"0");
            if (yanz != null){
                redisUtils.setStr(phone,yanz,60L);
                System.out.println(yanz);
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("no");
            }
        return transfer;

    }
    @RequestMapping("/register")
    public Transfer register( HttpServletResponse response, HttpServletRequest request,
                             String password,String code,String phone,String userName) {
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Transfer transfer = new Transfer();
        String yzm=(String) redisUtils.getKey(phone);
        if(yzm.equals(code)){
                User user=new User();
                user.setUsername(userName);
                user.setPassword(SecurityUtils.md5Hex(password));
                user.setPhone(phone);
                user.setTypeid(10);
            try {
                if(userService.add(user)){
                    transfer.setMessage("yes");
                }else{
                    transfer.setMessage("no");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            transfer.setMessage("noyzm");
        }
        return transfer;

    }

    @RequestMapping("/logout")
    public Transfer  logout(HttpServletRequest request,HttpServletResponse response,String token){
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Transfer transfer=new Transfer();
        try {
            redisUtils.del(token);
            transfer.setMessage("yes");
            System.out.println("退出成功");
        }catch (Exception e){
            e.printStackTrace();
            transfer.setMessage("no");
            System.out.println("退出失败");
        }
        return transfer;
    }

    @RequestMapping("/hasUserName")
    public Transfer  hasUserName(HttpServletRequest request,HttpServletResponse response,String userName){
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Transfer transfer=new Transfer();
        User user=null;
        try {
           user= userService.queryUserByUserName(userName);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(user==null){
            transfer.setMessage("yes");
        }else{
            transfer.setMessage("no");
        }
        return transfer;
    }
    @RequestMapping("/hasPhone")
    public Transfer  hasPhone(HttpServletRequest request,HttpServletResponse response,String phone){
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        Transfer transfer=new Transfer();
        User user=null;
        try {
            user= userService.queryUserByPhone(phone);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(user==null){
            transfer.setMessage("yes");
        }else{
            transfer.setMessage("no");
        }
        return transfer;
    }
}
